package cn.nfwx.tv.fragment.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import cn.nfwx.tv.R;
import cn.nfwx.tv.widget.CustomStatusView;

/**
 * Created by dell on 2018/1/19.
 */

public class WifiConnectDialogFragment extends DialogFragment {
    protected static final String EXTRA_NAME_CANCELABLE = "cancelable";

    protected TextView mMessageTv,cancle_btn,confirm_tv_tips;
    protected Button mConfirm;
    private CustomStatusView customStatusView;
    private View.OnClickListener mPositiveListener,mTryMoreListener;
    private StatusEnum mStatus;     //状态
    private int status=1;//1 Loading,2 LoadSuccess,3 LoadFailure
    public enum StatusEnum {
        Loading,
        LoadSuccess,
        LoadFailure
    }

    public static WifiConnectDialogFragment newInstance(int status) {

        WifiConnectDialogFragment instance = new WifiConnectDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("status",status);
        instance.setArguments(bundle);
        return instance;
    }


    public static WifiConnectDialogFragment newInstance() {

        WifiConnectDialogFragment instance = new WifiConnectDialogFragment();
        Bundle bundle = new Bundle();
        instance.setArguments(bundle);
        return instance;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        status=getArguments().getInt("status",1);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View view =View.inflate(getActivity(),R.layout.dialog_wifi_connect_success,null);
        initView(view);
        Dialog dialog=new Dialog(getActivity(),R.style.MyDialog);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0); //消除边距
        dialog.setContentView(view);
        WindowManager m = getActivity().getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = dialogWindow.getAttributes();
        p.height = WindowManager.LayoutParams.MATCH_PARENT;
        p.width = WindowManager.LayoutParams.MATCH_PARENT;
        dialogWindow.setAttributes(p);

        return dialog;
    }

    protected void initView(View view) {
        mMessageTv = findView(view,R.id.confirm_tv_message);
        mConfirm = findView(view,R.id.confirm_btn_positive);
        cancle_btn=findView(view,R.id.cancle_btn);
        customStatusView=findView(view,R.id.wifi_load);
        confirm_tv_tips=findView(view,R.id.confirm_tv_tips);
        init();
        if(status==1){
            loadLoading();
        }else if(status==2){
            loadSuccess();
        }else if(status==3){
            loadFailure();
        }

    }

    public void loadLoading(){
        status=1;
        if(customStatusView!=null) {
            cancle_btn.setVisibility(View.VISIBLE);
            confirm_tv_tips.setVisibility(View.GONE);
            mConfirm.setVisibility(View.GONE);
            mMessageTv.setText(getText(R.string.wifi_loading));
            customStatusView.loadLoading();
        }
    }

    public void loadSuccess(){
        status=2;
        if(customStatusView!=null){
            customStatusView.loadSuccess();
            mConfirm.setText(getText(R.string.wifi_ok));
            cancle_btn.setVisibility(View.GONE);
            confirm_tv_tips.setVisibility(View.VISIBLE);
            mConfirm.setVisibility(View.VISIBLE);
            mMessageTv.setText(getText(R.string.wifi_connect_success));
        }
    }

    public void loadFailure(){
        status=3;
        if(customStatusView!=null){
            customStatusView.loadFailure();
            mConfirm.setText(getText(R.string.try_more));
            confirm_tv_tips.setVisibility(View.GONE);
            mMessageTv.setText(getText(R.string.wifi_error));
            mConfirm.setVisibility(View.VISIBLE);
        }

    }

    private void init() {
        setCancelable(true);
        mConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 if(status==2){
                     if (mPositiveListener != null) {
                         mPositiveListener.onClick(v);
                     }
                 }else if(status==3){
                     if(mTryMoreListener!=null){
                         mTryMoreListener.onClick(v);
                     }
                 }
            }
        });
        cancle_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }



    public void setSureListener(View.OnClickListener mPositiveListener) {
        this.mPositiveListener = mPositiveListener;
    }
    public void setTryMoreListener(View.OnClickListener mPositiveListener) {
        this.mTryMoreListener = mPositiveListener;
    }




    public <T extends View> T findView(View view, int rid) {
        return (T) view.findViewById(rid);
    }

    protected int getGravity() {
        return Gravity.CENTER;
    }

    protected int getHeight() {
        return WindowManager.LayoutParams.MATCH_PARENT;
    }

    //是否透明背景
    protected boolean isTranslucentBackground() {
        return false;
    }

    //背景透明度
    protected float getDimAmount() {
        return -1;
    }

    /**
     * 是否填满整个屏幕
     */
    protected boolean isMatchParent() {
        return false;
    }

    protected boolean isOutsideTouchable() {
        return false;
    }

    /**
     * 是否透明状态栏
     */
    protected boolean isTranslucentStatus() {
        return true;
    }

    //自定义动画
    protected int getWindowAnimations() {
        return 0;
    }
}
